package com.norman.sorry.core.tiles;

import java.awt.Color;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import com.norman.sorry.core.pieces.IPiece;

/**
 * A helper class for creating a concrete version of a slide.
 * 
 * @author Brian Norman
 * @version 0.0.1 beta
 * @since 0.0.1 beta
 */
public abstract class AbstractSlide extends AbstractTile implements ISlide {

   /**
    * The color of the slide.
    */
   protected Color       color;

   /**
    * The tiles the slide covers.
    */
   protected List<ITile> slide;

   /**
    * Creates a new slide with the given color.
    * 
    * @param color
    *           the color of the slide.
    */
   public AbstractSlide(Color color) {
      super(false, false, false);
      this.color = color;
      slide = new LinkedList<ITile>();
      slide.add(this);
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.norman.sorry.core.tiles.ISlide#getColor()
    */
   @Override
   public Color getColor() {
      return color;
   }

   /**
    * {@inheritDoc}
    * 
    * Checks all the tiles of the slide, if any of them contain a game piece
    * then returns true.
    * 
    * @see com.norman.sorry.core.tiles.ISlide#containsPiece()
    */
   @Override
   public boolean containsPiece() {
      for (ITile l : slide) {
         if (l != this && l.containsPiece()) {
            return true;
         }
      }
      return super.containsPiece();
   }

   /**
    * Adds a tile to the end of the slide. This tile becomes the new end tile.
    * 
    * @param location
    *           the tile to add.
    */
   public void add(ITile location) {
      if (location != null) {
         slide.add(location);
      }
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.norman.sorry.core.tiles.ISlide#getSlide()
    */
   @Override
   public List<? extends ITile> getSlide() {
      return slide;
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.norman.sorry.core.tiles.ISlide#getStart()
    */
   @Override
   public ITile getStart() {
      if (slide.size() > 0) {
         return slide.get(0);
      } else {
         return this;
      }
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.norman.sorry.core.tiles.ISlide#getEnd()
    */
   @Override
   public ITile getEnd() {
      if (slide.size() > 0) {
         return slide.get(slide.size() - 1);
      } else {
         return null;
      }
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.norman.sorry.core.tiles.ISlide#canSlide(Color)
    */
   @Override
   public boolean canSlide(Color color) {
      return this.color != null && color != null && !this.color.equals(color);
   }

   /**
    * {@inheritDoc}
    * 
    * @see com.norman.sorry.core.tiles.ISlide#removePieces()
    */
   @Override
   public Collection<IPiece> removePieces() {
      Collection<IPiece> pieces = new LinkedList<IPiece>();
      for (ITile l : slide) {
         if (l.getPiece() != null) {
            pieces.add(l.getPiece());
            l.setPiece(null);
         }
      }
      return pieces;
   }

}
